package com.example.aussiehealthhelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class HealthDbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "HealthData.db";
    private static final int DATABASE_VERSION = 11; //版本号

    //药品表结构
    public static final String TABLE_DRUGS = "drugs";
    public static final String COLUMN_DRUG_ID = "_id";
    public static final String COLUMN_NAME_CN = "name_cn";
    public static final String COLUMN_NAME_EN = "name_en";
    public static final String COLUMN_INDICATION = "indication";
    public static final String COLUMN_INDICATION_CATEGORY = "indication_category";
    public static final String COLUMN_ADVERSE = "adverse_reaction";
    public static final String COLUMN_SIMILAR = "similar_drugs";
    public static final String COLUMN_DOSAGE = "dosage";
    public static final String COLUMN_NOTE = "note";
    public static final String COLUMN_IS_FAVORITE = "is_favorite";

    //创建药品表
    private static final String SQL_CREATE_DRUGS_TABLE =
            "CREATE TABLE " + TABLE_DRUGS + " (" +
                    COLUMN_DRUG_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                    COLUMN_NAME_CN + " TEXT NOT NULL," +
                    COLUMN_NAME_EN + " TEXT NOT NULL," +
                    COLUMN_INDICATION + " TEXT," +
                    COLUMN_INDICATION_CATEGORY + " TEXT," +
                    COLUMN_ADVERSE + " TEXT," +
                    COLUMN_SIMILAR + " TEXT," +
                    COLUMN_DOSAGE + " TEXT," +
                    COLUMN_NOTE + " TEXT," +
                    COLUMN_IS_FAVORITE + " INTEGER DEFAULT 0 CHECK (" +
                    COLUMN_IS_FAVORITE + " IN (0, 1)));";

    //所有适应症的大类
    public static final String[] INDICATION_CATEGORIES = {
            "感冒发烧类",
            "消化系统类",
            "皮肤外用类",
            "止痛消炎类",
            "抗过敏类",
            "五官科类",
            "心脑血管类",
            "泌尿生殖类",
            "神经系统类",
            "维生素矿物质类",
            "其他"
    };

    public HealthDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_DRUGS_TABLE);
        insertSampleDrugs(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //在升级时删除旧表并创建新表，重构数据
        if (oldVersion < 11) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_DRUGS);
            db.execSQL(SQL_CREATE_DRUGS_TABLE);
            insertSampleDrugs(db);
        }
    }

    //插入初始数据
    private void insertSampleDrugs(SQLiteDatabase db) {
        insertDrug(db, "对乙酰氨基酚片", "Paracetamol Tablets",
                "发热/轻中度疼痛", "感冒发烧类",
                "超量致急性肝衰竭（>10g/24h）；皮疹",
                "Panadol", "500-1000mg/次，间隔4-6小时，日限≤4000mg",
                "酒精↑肝毒性风险；日服≤8片/24h", false);

        insertDrug(db, "连花清瘟胶囊", "Lianhua Qingwen Capsules",
                "流感/发热/咳嗽", "感冒发烧类",
                "恶心,腹泻；无抗病毒循证依据",
                "Sambucol", "4粒/次，3次/日",
                "TGA限定为传统草药（ARTG 198079）", false);

        insertDrug(db, "氨咖黄敏胶囊", "Paracetamol/Caffeine/Chlorphenamine",
                "感冒/鼻塞/喷嚏/头痛", "感冒发烧类",
                "嗜睡,QT间期延长",
                "Codral Original", "2粒/次，2次/日",
                "含扑尔敏，驾驶前禁用（TGA警示）", false);

        insertDrug(db, "复方甘草片", "Compound Liquorice Tablets",
                "咳嗽", "感冒发烧类",
                "假性醛固酮增多症（长期用）；水钠潴留",
                "Bisolvon Dry", "1-2片/次，3次/日；≤7天",
                "澳洲列为S3管控药（需药师监督）", false);

        insertDrug(db, "双黄连口服液", "Shuanghuanglian Oral Liquid",
                "外感风热/发热", "感冒发烧类",
                "过敏反应（呼吸困难/皮疹）",
                "Kaloba", "10ml/次，3次/日",
                "2018年CFDA过敏警示通告", false);

        insertDrug(db, "头孢克肟胶囊", "Cefixime Capsules",
                "呼吸道感染/泌尿道感染/中耳炎", "感冒发烧类",
                "腹泻,皮疹；严重过敏反应",
                "Cephalexin (Keflex)", "成人200mg/日，分1-2次；儿童8mg/kg/日",
                "青霉素过敏者禁用", false);

        insertDrug(db, "板蓝根颗粒", "Isatis root granules",
                "感冒/流感/上呼吸道感染", "感冒发烧类",
                "胃肠不适；过敏反应（呼吸困难/皮疹）",
                "Sambucol", "1袋/次，3次/日",
                "TGA列为Listed Medicine（AUST L 108542）", false);

        insertDrug(db, "铝碳酸镁咀嚼片", "Aluminium Hydroxide Chewable Tabs",
                "反酸/烧心/胃灼热", "消化系统类",
                "便秘,铝蓄积（肾损伤者）",
                "Mylanta", "咀嚼1-2片/次（餐后1h），日极量12片",
                "澳洲无铝碳酸镁，氢氧化铝为等效替代", false);

        insertDrug(db, "蒙脱石散", "Smectite Powder",
                "急性腹泻/水样便/慢性腹泻", "消化系统类",
                "便秘（减量可逆）",
                "Diastop", "成人首剂4g，后2g/次；儿童按体重调整（50mg/kg/日）",
                "澳洲用洛哌丁胺（Diastop）", false);

        insertDrug(db, "多潘立酮片", "Domperidone Tablets",
                "恶心/呕吐/胃胀气/消化不良", "消化系统类",
                "QT间期延长→猝死风险（>30mg/日）",
                "Motilium", "10mg/次，3次/日；≤7天",
                "澳洲列为处方药（S4），心脏病史绝对禁用", false);

        insertDrug(db, "奥美拉唑肠溶片", "Omeprazole Delayed-Release Capsules",
                "胃溃疡/十二指肠溃疡/反流性食管炎", "消化系统类",
                "长期用→骨折/低镁血症/艰难梭菌感染风险↑",
                "Losec", "胃溃疡：20mg/日；GERD：20-40mg/日",
                "与氯吡格雷同用↓抗血小板效果（FDA黑框）", false);

        insertDrug(db, "乳酶生片", "Lactase Tablets",
                "乳糖不耐/消化不良/腹胀", "消化系统类",
                "无严重不良反应",
                "Lacteeze", "餐前15min服1-2片",
                "无", false);

        insertDrug(db, "莫匹罗星软膏", "Mupirocin 2% Ointment",
                "脓疱疮/毛囊炎/皮肤感染", "皮肤外用类",
                "接触性皮炎（<1%）；耐药金葡菌无效",
                "Bactroban", "涂患处3次/日，≤10天",
                "鼻腔用需专用鼻用剂型（TGA区分）", false);

        insertDrug(db, "酮康唑乳膏", "Ketoconazole 2% Cream",
                "体股癣/花斑癣/头皮糠疹", "皮肤外用类",
                "局部灼烧感；肝毒性警告（口服剂型）",
                "Nizoral Cream", "1-2次/日，疗程2-4周",
                "澳洲已撤销口服剂型（2015肝毒性）", false);

        insertDrug(db, "炉甘石洗剂", "Calamine Lotion",
                "痱子/虫咬/荨麻疹/皮肤瘙痒", "皮肤外用类",
                "皮肤干燥（敏感肌）",
                "Calamine LBP", "摇匀涂患处，3-4次/日",
                "渗出性皮炎禁用（加重刺激）", false);

        insertDrug(db, "复方地塞米松乳膏", "Hydrocortisone 1%+Miconazole",
                "皮炎/湿疹/银屑病/皮肤瘙痒", "皮肤外用类",
                "皮肤萎缩（面部连续>1周）；继发感染",
                "Daktacort", "薄涂1-2次/日，≤7天",
                "1%氢化可的松为澳洲OTC标准", false);

        insertDrug(db, "烧烫伤膏", "Burn Ointment",
                "烫伤/烧伤/热力损伤/创面护理", "皮肤外用类",
                "局部刺激；感染风险",
                "Burnaid", "清创后涂敷，1-2次/日",
                "深II度以上烧伤需就医", false);

        insertDrug(db, "布洛芬缓释胶囊", "Ibuprofen Sustained-Release",
                "关节痛/肌肉痛/牙痛/头痛", "止痛消炎类",
                "消化道出血/穿孔；心血管事件风险↑",
                "Nurofen SR", "300mg/12h；日极量1200mg",
                ">65岁/心血管病史慎用（FDA黑框）", false);

        insertDrug(db, "双氯芬酸钠肠溶片", "Diclofenac Enteric-Coated Tablets",
                "类风湿关节炎/强直性脊柱炎/痛经", "止痛消炎类",
                "肝损伤（监测ALT）；再生障碍性贫血（罕见）",
                "Voltaren EC", "75-150mg/日，分2-3次",
                "肠溶剂型不可压碎", false);

        insertDrug(db, "氨基葡萄糖胶囊", "Glucosamine Sulfate",
                "骨关节炎/软骨损伤/关节僵硬", "止痛消炎类",
                "腹胀/过敏（甲壳类过敏者慎用）",
                "Glucosamine S-Forte", "1500mg/日，分2次",
                "非治愈性治疗；疗效个体差异大", false);

        insertDrug(db, "萘普生片", "Naproxen Tablets",
                "急性痛风/关节炎/肌肉骨骼痛", "止痛消炎类",
                "耳鸣（超量信号）；肾损伤",
                "Naprogesic", "首剂750mg，后250mg/8h；≤5天",
                "活动性消化道溃疡禁用", false);

        insertDrug(db, "氯雷他定片", "Loratadine Tablets",
                "过敏性鼻炎/荨麻疹/皮肤瘙痒", "抗过敏类",
                "嗜睡（<4%）；心悸（超量）",
                "Claratyne", "10mg/日",
                "与酮康唑同用需↓剂量50%（CYP3A4抑制）", false);

        insertDrug(db, "盐酸西替利嗪片", "Cetirizine Hydrochloride Tablets",
                "慢性荨麻疹/过敏性结膜炎/花粉症", "抗过敏类",
                "镇静作用弱于一代；口干",
                "Zyrtec", "5-10mg/日",
                "肾损者剂量减半（eGFR<30mL/min）", false);

        insertDrug(db, "糠酸莫米松鼻喷雾剂", "Mometasone Furoate Nasal Spray",
                "过敏性鼻炎/鼻塞/打喷嚏", "抗过敏类",
                "鼻出血/鼻中隔穿孔（长期高剂量）",
                "Nasonex", "2喷/鼻孔/日",
                "使用前摇匀，疗程≤3个月", false);

        insertDrug(db, "左氧氟沙星滴眼液", "Levofloxacin 0.5% Eye Drops",
                "细菌性结膜炎/细菌性角膜炎/眼部感染", "五官科类",
                "结晶性角膜病（超7日）",
                "Oftaquix", "急性期：1-2滴/2h；缓解后4次/日",
                "角膜溃疡需联用全身药物", false);

        insertDrug(db, "妥布霉素地塞米松眼膏", "Tobramycin Dexamethasone Eye Oint",
                "眼部细菌性炎症/术后感染/眼睑炎", "五官科类",
                "眼压升高（激素成分）；角膜真菌感染",
                "Tobradex", "0.5cm膏体涂结膜囊，3-4次/日",
                "激素使用≤7天；角膜损伤禁用", false);

        insertDrug(db, "人工泪液", "Hypromellose Eye Drops",
                "干眼症状/眼疲劳/隐形眼镜不适", "五官科类",
                "一过性视物模糊",
                "Systane", "1-2滴/次，4-6次/日",
                "开封后30天弃用", false);

        insertDrug(db, "佐匹克隆片", "Zopiclone Tablets",
                "入睡困难/夜间醒来/睡眠质量差", "神经系统类",
                "次日操作能力↓；依赖（>4周）",
                "Imovane", "3.75-7.5mg/睡前服",
                "次晨禁止驾驶/操作机械", false);

        insertDrug(db, "维生素C片", "Ascorbic Acid Tablets",
                "坏血病/促进铁吸收/免疫力低下", "维生素矿物质类",
                "肾结石（>1g/日）；渗透性腹泻",
                "Cenovis VC", "坏血病：300mg/日；缺铁辅助：200mg/次",
                "尿酸盐/草酸盐结石史慎用", false);

        insertDrug(db, "碳酸钙D3片", "Calcium + Vit D3 Tablets",
                "骨质疏松/骨软化/佝偻病", "维生素矿物质类",
                "嗳气/高钙血症（>2500mg/d）",
                "Ostelin Vita D3", "钙500-600mg/次，1-2次/日（随餐）",
                "抑酸药↓吸收；需联用Vit D", false);

        insertDrug(db, "多维元素片", "Multivitamin Formula",
                "营养补充/妊娠哺乳/代谢需求增加", "维生素矿物质类",
                "胃肠不适",
                "Elevit", "1片/日",
                "澳洲孕产专用配方（含叶肥800μg）", false);

        insertDrug(db, "硫酸亚铁片", "Ferrous Sulfate Tablets",
                "缺铁性贫血/慢性失血/吸收不良", "维生素矿物质类",
                "黑便/便秘/恶心（30%患者）",
                "Ferro-Grad", "120mg元素铁/日；餐后服",
                "忌与茶/咖啡同服；与维生素C同服促吸收", false);
    }

    private long insertDrug(SQLiteDatabase db, String nameCN, String nameEN,
                            String indication, String indicationCategory,
                            String adverse, String similar,
                            String dosage, String note, boolean isFavorite) {
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME_CN, nameCN);
        values.put(COLUMN_NAME_EN, nameEN);
        values.put(COLUMN_INDICATION, indication);
        values.put(COLUMN_INDICATION_CATEGORY, indicationCategory);
        values.put(COLUMN_ADVERSE, adverse == null ? "" : adverse);
        values.put(COLUMN_SIMILAR, similar);
        values.put(COLUMN_DOSAGE, dosage);
        values.put(COLUMN_NOTE, note == null ? "" : note);
        values.put(COLUMN_IS_FAVORITE, isFavorite ? 1 : 0);

        return db.insert(TABLE_DRUGS, null, values);
    }

    //根据适应症类型获取药品
    public List<Drug> getDrugsByCategory(String category) {
        List<Drug> drugList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        try (Cursor cursor = db.query(
                TABLE_DRUGS,
                null,
                COLUMN_INDICATION_CATEGORY + " = ?",
                new String[]{category},
                null,
                null,
                COLUMN_NAME_CN + " ASC"
        )) {
            while (cursor.moveToNext()) {
                Drug drug = createDrugFromCursor(cursor);
                if (drug != null) drugList.add(drug);
            }
        } catch (Exception e) {
            Log.e("HealthDbHelper", "按类型获取药品错误: " + e.getMessage());
        }
        return drugList;
    }

    //获取所有适应症类别
    public List<String> getAllIndicationCategories() {
        SQLiteDatabase db = getReadableDatabase();
        List<String> categories = new ArrayList<>();

        String query = "SELECT DISTINCT " + COLUMN_INDICATION_CATEGORY +
                " FROM " + TABLE_DRUGS +
                " WHERE " + COLUMN_INDICATION_CATEGORY + " IS NOT NULL " +
                " AND " + COLUMN_INDICATION_CATEGORY + " != ''";

        try (Cursor cursor = db.rawQuery(query, null)) {
            while (cursor.moveToNext()) {
                String category = cursor.getString(0);
                if (category != null && !category.trim().isEmpty()) {
                    categories.add(category);
                }
            }
        }

        //确保默认类别都存在
        for (String defaultCategory : INDICATION_CATEGORIES) {
            if (!categories.contains(defaultCategory)) {
                categories.add(defaultCategory);
            }
        }

        return categories;
    }

    public List<Drug> getAllDrugs() {
        return getDrugs(null, null);
    }

    public List<Drug> getFavoriteDrugs() {
        String selection = COLUMN_IS_FAVORITE + " = 1";
        return getDrugs(selection, null);
    }

    private List<Drug> getDrugs(String selection, String[] selectionArgs) {
        List<Drug> drugList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        try (Cursor cursor = db.query(
                TABLE_DRUGS,
                null,
                selection,
                selectionArgs,
                null,
                null,
                COLUMN_NAME_CN + " ASC"
        )) {
            while (cursor.moveToNext()) {
                Drug drug = createDrugFromCursor(cursor);
                if (drug != null) drugList.add(drug);
            }
        } catch (Exception e) {
            Log.e("HealthDbHelper", "获取药品错误: " + e.getMessage());
        }
        return drugList;
    }

    public Drug getDrugById(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        try (Cursor cursor = db.query(
                TABLE_DRUGS,
                null,
                COLUMN_DRUG_ID + " = ?",
                new String[]{String.valueOf(id)},
                null,
                null,
                null
        )) {
            if (cursor != null && cursor.moveToFirst()) {
                return createDrugFromCursor(cursor);
            }
        } catch (Exception e) {
            Log.e("HealthDbHelper", "按ID获取药品错误: " + e.getMessage());
        }
        return null;
    }

    public List<Drug> searchDrugsByName(String query) {
        List<Drug> drugList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        String selection = COLUMN_NAME_CN + " LIKE ? OR " +
                COLUMN_NAME_EN + " LIKE ?";
        String[] selectionArgs = new String[]{"%" + query + "%", "%" + query + "%"};

        try (Cursor cursor = db.query(
                TABLE_DRUGS,
                null,
                selection,
                selectionArgs,
                null,
                null,
                COLUMN_NAME_CN + " ASC"
        )) {
            while (cursor.moveToNext()) {
                Drug drug = createDrugFromCursor(cursor);
                if (drug != null) drugList.add(drug);
            }
        } catch (Exception e) {
            Log.e("HealthDbHelper", "按名称搜索药品错误: " + e.getMessage());
        }
        return drugList;
    }

    public List<Drug> findDrugsBySymptoms(List<String> symptoms) {
        if (symptoms == null || symptoms.isEmpty()) return new ArrayList<>();

        SQLiteDatabase db = getReadableDatabase();
        StringBuilder query = new StringBuilder(
                "SELECT DISTINCT * FROM " + TABLE_DRUGS + " WHERE "
        );

        List<String> conditions = new ArrayList<>();
        List<String> selectionArgs = new ArrayList<>();

        for (String symptom : symptoms) {
            conditions.add(COLUMN_INDICATION + " LIKE ?");
            selectionArgs.add("%" + symptom + "%");
        }

        query.append(String.join(" OR ", conditions));

        try (Cursor cursor = db.rawQuery(query.toString(), selectionArgs.toArray(new String[0]))) {
            List<Drug> drugs = new ArrayList<>();
            while (cursor.moveToNext()) {
                Drug drug = createDrugFromCursor(cursor);
                if (drug != null) drugs.add(drug);
            }
            return drugs;
        }
    }

    //添加药品
    public long addDrug(Drug drug) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COLUMN_NAME_CN, drug.getNameCN());
        values.put(COLUMN_NAME_EN, drug.getNameEN());
        values.put(COLUMN_INDICATION, drug.getIndication());
        values.put(COLUMN_INDICATION_CATEGORY, drug.getIndicationCategory());
        values.put(COLUMN_ADVERSE, drug.getAdverseReaction());
        values.put(COLUMN_SIMILAR, drug.getSimilarDrugs());
        values.put(COLUMN_DOSAGE, drug.getDosage());
        values.put(COLUMN_NOTE, drug.getNote());
        values.put(COLUMN_IS_FAVORITE, drug.getIsFavorite() ? 1 : 0);

        try {
            return db.insert(TABLE_DRUGS, null, values);
        } catch (Exception e) {
            Log.e("HealthDbHelper", "添加药品错误: " + e.getMessage());
            return -1;
        }
    }

    //更新药品
    public int updateDrug(Drug drug) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(COLUMN_NAME_CN, drug.getNameCN());
        values.put(COLUMN_NAME_EN, drug.getNameEN());
        values.put(COLUMN_INDICATION, drug.getIndication());
        values.put(COLUMN_INDICATION_CATEGORY, drug.getIndicationCategory());
        values.put(COLUMN_ADVERSE, drug.getAdverseReaction());
        values.put(COLUMN_SIMILAR, drug.getSimilarDrugs());
        values.put(COLUMN_DOSAGE, drug.getDosage());
        values.put(COLUMN_NOTE, drug.getNote());
        values.put(COLUMN_IS_FAVORITE, drug.getIsFavorite() ? 1 : 0);

        try {
            return db.update(
                    TABLE_DRUGS,
                    values,
                    COLUMN_DRUG_ID + " = ?",
                    new String[]{String.valueOf(drug.getId())}
            );
        } catch (Exception e) {
            Log.e("HealthDbHelper", "更新药品错误: " + e.getMessage());
            return 0;
        }
    }

    public int deleteDrug(int drugId) {
        SQLiteDatabase db = this.getWritableDatabase();
        try {
            return db.delete(
                    TABLE_DRUGS,
                    COLUMN_DRUG_ID + " = ?",
                    new String[]{String.valueOf(drugId)}
            );
        } catch (Exception e) {
            Log.e("HealthDbHelper", "删除药品错误: " + e.getMessage());
            return 0;
        }
    }

    public int toggleFavoriteStatus(int drugId, boolean isFavorite) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_IS_FAVORITE, isFavorite ? 1 : 0);

        try {
            return db.update(
                    TABLE_DRUGS,
                    values,
                    COLUMN_DRUG_ID + " = ?",
                    new String[]{String.valueOf(drugId)}
            );
        } catch (Exception e) {
            Log.e("HealthDbHelper", "切换收藏状态错误: " + e.getMessage());
            return 0;
        }
    }

    public List<Drug> getAllFavoriteDrugs() {
        List<Drug> drugList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        String selection = COLUMN_IS_FAVORITE + " = 1";

        try (Cursor cursor = db.query(
                TABLE_DRUGS,
                null,
                selection,
                null,
                null,
                null,
                COLUMN_NAME_CN + " ASC"
        )) {
            while (cursor.moveToNext()) {
                Drug drug = createDrugFromCursor(cursor);
                if (drug != null) drugList.add(drug);
            }
        } catch (Exception e) {
            Log.e("HealthDbHelper", "获取收藏药品错误: " + e.getMessage());
        }
        return drugList;
    }


    private Drug createDrugFromCursor(Cursor cursor) {
        try {
            return new Drug(
                    cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_DRUG_ID)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME_CN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME_EN)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_INDICATION)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_INDICATION_CATEGORY)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_ADVERSE)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_SIMILAR)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_DOSAGE)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NOTE)),
                    cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_FAVORITE)) == 1
            );
        } catch (IllegalArgumentException e) {
            Log.e("HealthDbHelper", "创建药品对象错误: " + e.getMessage());
            return null;
        }
    }
}